/*
 * Created by Icodeon Ltd.
 * Date: 10/05/2008
 * 
 * Copyright 2008 All Rights Reserved
 * Icodeon Ltd 
 * St Johns Innovation Centre
 * Cowley Road
 * Cambridge 
 * CB4 0WS England
 * 
 * Registered in England and Wales No: 5068195
 * Use is subject to license terms.
 */

using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace Icodeon.Xml
{
    public class XsltTransformer
    {
        public void Transform(FileInfo xmlPath, FileInfo xslPath, FileInfo outPath)
        {
            /*
             * Deprecated code
             * 
             * warning CS0618: 'System.Xml.Xsl.XslTransform' is obsolete: 
             * 'This class has been deprecated. Please use 
             * System.Xml.Xsl.XslCompiledTransform instead.
             * 
            // Load the Xml
            XPathDocument xPathDocument = new XPathDocument(xmlPath);
            XslTransform xslTransform = new XslTransform();

            // Load the Xsl 
            xslTransform.Load(xslPath);

            // Create the output stream
            XmlTextWriter xmlTextWriter = new XmlTextWriter(outPath, null);

            // Transform of Xml
            xslTransform.Transform(xPathDocument, null, xmlTextWriter);
            xmlTextWriter.Close();
             */

            XmlTextWriter xmlTextWriter = null;
            Exception rethrow = null;
            try
            {

                XslCompiledTransform xslt = new XslCompiledTransform();
                XsltSettings xsltSettings = new XsltSettings();
                xsltSettings.EnableDocumentFunction = true;
                xslt.Load(xslPath.FullName, xsltSettings, null);
                //xslt.Load(xslPath.FullName);
                xmlTextWriter = new XmlTextWriter(outPath.FullName, new UTF8Encoding(false));
                xslt.Transform(xmlPath.FullName, xmlTextWriter);
            }
            catch (Exception e)
            {
                rethrow = e;
            }
            finally
            {
                xmlTextWriter.Flush();
                xmlTextWriter.Close();
                if (xmlTextWriter.BaseStream != null)
                {
                    xmlTextWriter.BaseStream.Flush();
                    xmlTextWriter.BaseStream.Close();
                }
                if (rethrow != null)
                {
                    throw rethrow;
                }

            }

            //xslt.Load(xslPath.FullName);

            /*
            // Execute the transform and output the results to a file.
            xslt.Transform(xmlPath.FullName, outPath.FullName);

            XslCompiledTransform xslt = new XslCompiledTransform();
            XsltSettings xsltSettings = new XsltSettings();
            // it complains if we don't do this (I guess it's a security feature)
            xsltSettings.EnableDocumentFunction = true;
            xslt.Load(xslFileName, xsltSettings, null);
            // The output encoding specified in alphasign.xsl is UTF-8.
            // By default Transform includes a Byte Order Mark, which we do not want.
            // We fix that by passing false to the UTF8Encoding constructor.
            // Alternatively, alphasign.xsl could specify <xsl:output encoding="ASCII"/>,
            // which is actually more appropriate for the Alpha sign, but that causes
            // the transform to fail completely when the input text contains non-ASCII
            // characters.  I prefer to let it output a few garbage characters rather 
            // than fail completely.
            XmlTextWriter writer = new XmlTextWriter(stream, new UTF8Encoding(false));
            xslt.Transform(xmlFileName, writer);
             */



        }
    }
}
